$(function() {
	var allorder = echarts.init(document.getElementById('allorder'));
	var allmoneny = echarts.init(document.getElementById('allmoneny'));
	var alltime = echarts.init(document.getElementById('alltime'));
	$(this).removeClass('am-btn-secondary')
	$('.timeclassify>button').on('click', function() {
		$('.time').val($(this).data('val'))
	})

	option = {
		tooltip: {
			trigger: 'item',
			formatter: "{a} <br/>{b}: {c} ({d}%)"
		},
		legend: {
			orient: 'vertical',
			x: 'left',
			data: [
			{foreach $types as $key => $type}
			'{$type}',
			{/foreach}
			'']
		},
		series: [{
			name: '订单数量',
			type: 'pie',
			radius: ['50%', '70%'],
			avoidLabelOverlap: false,
			label: {
				normal: {
					show: false,
					position: 'center'
				},
				emphasis: {
					show: true,
					textStyle: {
						fontSize: '24',
						fontWeight: 'bold'
					}
				}
			},
			labelLine: {
				normal: {
					show: false
				}
			},
			data: [
				{foreach $types as $key => $type}
				{
					value: {$qty[$key]?$qty[$key]:0},
					name: '{$type}'
				},
				{/foreach}
				{}
			]
		}]
	};

	option1 = {
		tooltip: {
			trigger: 'item',
			formatter: "{a} <br/>{b}: {c} ({d}%)"
		},
		legend: {
			orient: 'vertical',
			x: 'left',
			data: [
			{foreach $types as $key => $type}
			'{$type}',
			{/foreach}
			'']
		},
		series: [{
			name: '金额',
			type: 'pie',
			radius: ['50%', '70%'],
			avoidLabelOverlap: false,
			label: {
				normal: {
					show: false,
					position: 'center'
				},
				emphasis: {
					show: true,
					textStyle: {
						fontSize: '24',
						fontWeight: 'bold'
					}
				}
			},
			labelLine: {
				normal: {
					show: false
				}
			},
			data: [
			{foreach $types as $key => $type}
				{
					value: {$price[$key]?$price[$key]:0},
					name: "{$type}"
				},
					{/foreach}
				{}
			]
		}]
	};
	option2 = {
		tooltip: {
			trigger: 'item',
			formatter: "{a} <br/>{b}: {c} ({d}%)"
		},
		legend: {
			orient: 'vertical',
			x: 'left',
			data: [
			{foreach $types as $key => $type}
			'{$type}',
			{/foreach}
			'']
		},
		series: [{
			name: '耗时',
			type: 'pie',
			radius: ['50%', '70%'],
			avoidLabelOverlap: false,
			label: {
				normal: {
					show: false,
					position: 'center'
				},
				emphasis: {
					show: true,
					textStyle: {
						fontSize: '24',
						fontWeight: 'bold'
					}
				}
			},
			labelLine: {
				normal: {
					show: false
				}
			},
			data: [
				{foreach $types as $key => $type}
				{
					value: {$time[$key]?$time[$key]:0},
					name: "{$type}"
				},
					{/foreach}
				{}
				
			]
		}]
	};

	// 使用刚指定的配置项和数据显示图表。
	allorder.setOption(option);
	allmoneny.setOption(option1);
	alltime.setOption(option2);
	
//		$('.report-classify>button').on('click', function() {
//			$(this).removeClass('am-btn-secondary')
//			$(this).addClass('am-btn-success')
//			$(this).siblings().removeClass('am-btn-success')
//			$(this).siblings().addClass('am-btn-secondary')
//		})
		
	$('.endtime').on('change',function(){
		datejudge($('.begintime'),$('.endtime'))
        checkCurrentTag();
	})
	$('.begintime').on('change',function(){
		datejudge($('.begintime'),$('.endtime'))
        checkCurrentTag();
	})

    Date.prototype.format =function(format)
    {
        var o = {
            "M+" : this.getMonth()+1, //month
            "d+" : this.getDate(),    //day
            "h+" : this.getHours(),   //hour
            "m+" : this.getMinutes(), //minute
            "s+" : this.getSeconds(), //second
            "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
            "S" : this.getMilliseconds() //millisecond
        }
        if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
            (this.getFullYear()+"").substr(4- RegExp.$1.length));
        for(var k in o)if(new RegExp("("+ k +")").test(format))
            format = format.replace(RegExp.$1,
                RegExp.$1.length==1? o[k] :
                    ("00"+ o[k]).substr((""+ o[k]).length));
        return format;
    }

    $('.auto-change-time').on('click', function(){
    	let time = $(this).data('time');

		let start = getTimeByTag(time);
		let end = new Date().format('yyyy-MM-dd');

        $('.begintime').val(start);
        $('.endtime').val(end);

        checkCurrentTag();

        $('#query').click();
	});

    checkCurrentTag();
	// 显示当前选中的时间标志
	function checkCurrentTag() {
		let inputStart = $('.begintime').val();
		let inputEnd = $('.endtime').val();
		let today = new Date().format('yyyy-MM-dd');

        $('.auto-change-time').each(function(){
        	let that = $(this);
        	let timeTag = that.data('time');

        	if(inputEnd === today && inputStart === getTimeByTag(timeTag)){
        		that.addClass('am-btn-success');
			}else{
        		that.removeClass('am-btn-success');
			}
		});
    }

	// 根据时间标记获取时间
	function getTimeByTag(tag) {
        let format = '';
        switch (tag) {
            case 'day':
                format = 'yyyy-MM-dd';
                break;

            case 'month':
                format = 'yyyy-MM-01';
                break;

            case 'year':
                format = 'yyyy-01-01';
                break;
            default:
                format = 'yyyy-01-01';
                break;
        }

        return new Date().format(format);
    }

})